package 数据结构OJ;

public class 链表的中间节点_07 {

//  Definition for singly-linked list.
  public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }

    public ListNode middleNode(ListNode head) {
        if(head.next == null)
            return head;
        ListNode fast = head;
        ListNode slow = head;
        //判断条件为fast ,fast.next,
        //可以在节点为偶数个链表时比奇数链表多进入一次循环，
        // 从而使slow多向后指一个节点
        //
        //而非fast.next,fast.next.next
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}